package com.nf.health.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.models.Question;

import java.util.List;

public class QuestionAdapter extends BaseAdapter {
	private Context context;
	private List<Question> question;

	String a;
	public QuestionAdapter(Context context, List<Question> question) {
		super();
		this.context = context;
		this.question = question;
	}
	@Override
	public int getCount() {
		if(question != null){
			return question.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return question.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView==null) {
			convertView = View.inflate(context,R.layout.item_question,null);
			holder = new ViewHolder();
			holder.question_title = (TextView) convertView.findViewById(R.id.question_title);
			holder.yes_radio = (RadioButton) convertView.findViewById(R.id.yes_radio);
			holder.no_radio = (RadioButton) convertView.findViewById(R.id.no_radio);
			holder.gendergroup = (RadioGroup) convertView.findViewById(R.id.gendergroup);


			convertView.setTag(holder);	
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(question.get(position).getAnswer().equals("")){
		}else{
			//已经回答过了
			if(question.get(position).getAnswer().equals("1")){
				holder.yes_radio.setChecked(true); 
			}
			if(question.get(position).getAnswer().equals("2")){
				holder.no_radio.setChecked(true); 
			}
			holder.yes_radio.setClickable(false);
			holder.no_radio.setClickable(false);
		}
		holder.question_title.setText(question.get(position).getQuestion());
		holder.yes_radio.setText("  "+question.get(position).getOption().get("1"));
		holder.no_radio.setText("  "+question.get(position).getOption().get("2"));
		holder.gendergroup.setTag(question.get(position).getNum());


		holder.gendergroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
				if(arg1==R.id.yes_radio){
					listener.onClick(position+1,"1");
				}
				if(arg1==R.id.no_radio){
					listener.onClick(position+1,"2");
				}
			}
		});






		return convertView;

	}
	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){
		this.listener = listener;
	}

	public class ViewHolder{
		TextView question_title;
		RadioButton yes_radio,no_radio;
		RadioGroup gendergroup;
		
	}
}
